//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================

#ifndef RENDM5D_SCENE_H
#define RENDM5D_SCENE_H

#include "m5d_scene.h"
#include "rend_device.h"
#include "rendm5d_mesh.h"
#include "rendm5d_pntsprt.h"

//==================================================================
namespace RendM5D {

//==================================================================
class SceneStats
{
	int	_total_frames;
	int	_total_models;
	int	_total_meshes;
	int	_total_rendered;
public:
	SceneStats() :
		_total_frames(0),
		_total_models(0),
		_total_meshes(0),
		_total_rendered(0)
	{
	}
};

//==================================================================
class Scene : public M5D::Scene
{
private:
	RendDevice			*_RDevicep;

	M5D::Camera			*_cur_mcamerap;
	M5D::Camera			*_cur_secondary_camp;
	RendShader::Param	_cur_secondary_cam_lproj_obj_m_shparam;

	//M5D::Light		*_cur_main_lightp;
	PMath::Vector4	_cur_main_light_ws_pos;
	PMath::Vector4	_cur_main_light_view_pos;
	SceneStats		_stats;

//	PMath::Matrix44	_cur_cam_invmat;
//	PMath::Matrix44	_cur_proj_world_m;

	int			_rend_off_x;
	int			_rend_off_y;

public:
	Scene( RendDevice *rdevicep, const char *namep=0, const TCHAR *dirpathp=NULL );
	~Scene();

	//static void SuperDeleteCB_s( M5D::Scene *mscenep, void *objp );

	void SelectCamera( M5D::Camera *camerap )
	{
		_cur_mcamerap = camerap;
	}

	M5D::Camera *GetSelectedCamera()
	{
		return _cur_mcamerap;
	}

	void SetRendOffset( int x, int y )
	{
		_rend_off_x = x;
		_rend_off_y = y;
	}

	void PreRender( RendM5D::Params &rparams );
	void Render( RendM5D::Params &rparams );
/*
	void RenderOnOff( const u_char *tri_onoffmap, const float on_col[4], const float off_col[4] );
	void RenderBBox();
	void RenderPlainOneFace( bool use_lights=true, const M5D::Mesh *meshp=NULL, int tri_idx=0 );
*/
	void CalcRendBox( float bbmin[2], float bbmax[2] );

private:
	void	prepareLights();
	void	prepareCamera();
	void	prepareRenderables( RendM5D::Params &rparams );
	void	prepareMeshTransforms( RendM5D::Mesh *rmeshp, const PMath::Matrix44 &world_obj_m, const PMath::Matrix44 lightmap_rgb_mats[3] );
	void	preparePntSprtTransforms( RendM5D::PntSprt *rpntsprtp, const PMath::Matrix44 &world_m );
	
	void	selectRendMesh( RendM5D::Mesh *rmeshp, const RendM5D::Params &rparams );
	void	selectRendPntSprt( RendM5D::PntSprt *robjp, const RendM5D::Params &rparams );

	void	renderOpaqueCustomMesh( RendM5D::Params &rparams );
	void	renderParticles( RendM5D::Params &rparams );
	void	renderStandardMeshOpaquePass( RendM5D::Params &rparams );
	void	renderStandardMeshTranslucentPass( RendM5D::Params &rparams );

private:
	virtual void	OnTextureCreate( M5D::Texture *objp );
	virtual void	OnTextureDelete( M5D::Texture *objp );
	virtual void	OnMaterialCreate( M5D::Material *objp );
	virtual void	OnMaterialDelete( M5D::Material *objp );
	virtual void	OnModelCreate( M5D::Model *objp );
	virtual void	OnModelDelete( M5D::Model *objp );
	virtual void	OnDispListCreate( M5D::DispList *objp );
	virtual void	OnDispListDelete( M5D::DispList *objp );
};

};

#endif
